package jju.game;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;

class Hero extends JPanel {
    private int x;
    private int y;
    int r;
    private int play;

    private int speedX;
    private int speedY;

    final public int maxSpeed = 2;

    Const value;

    public int getX(){
        return x;
    }

    public int getY(){
        return y;
    }

    Hero(int x, int y, int play){
        this.x = x;
        this.y = y;
        this.play = play;
        r = 30;
        speedX = speedY = 0;
        MoveThread.start();
    }

    public void paint(Graphics g){
        this.setBackground(new Color(255, 255, 228));
        if (play == 1){
            g.setColor(new Color(123, 194, 219));
        } else {
            g.setColor(new Color(255, 255, 228));
        }

        g.fillOval(x,y,r,r);

    }

    public void setSpeedY(int step) {
        if (step > 0 && speedY < maxSpeed){
            speedY += step;
        }
        if (step < 0 && speedY > -1*maxSpeed){
            speedY += step;
        }
    }

    public void setSpeedX(int step){
        if (step > 0 && speedX < maxSpeed){
            speedX += step;
        }
        if (step < 0 && speedX > -1*maxSpeed){
            speedX += step;
        }
    }

    public void pressAction(int key){
        int step = 0;
        if (play == 1){
            if (key == KeyEvent.VK_W){
                step = -1;
                setSpeedY(step);
            }
            if (key == KeyEvent.VK_S){
                step = 1;
                setSpeedY(step);
            }
            if (key == KeyEvent.VK_A){
                step = -1;
                setSpeedX(step);
            }
            if (key == KeyEvent.VK_D){
                step = 1;
                setSpeedX(step);
            }
        } else if (play == 2){
            if (key == KeyEvent.VK_UP){
                step = -1;
                setSpeedY(step);
            }
            if (key == KeyEvent.VK_DOWN){
                step = 1;
                setSpeedY(step);
            }
            if (key == KeyEvent.VK_LEFT){
                step = -1;
                setSpeedX(step);
            }
            if (key == KeyEvent.VK_RIGHT){
                step = 1;
                setSpeedX(step);
            }
        }

    }

    Thread MoveThread = new Thread(){
        @Override
        public void run() {
            while (true){
                if (x > 0 && x+r+speedX< 1500 && y > 30 && y+r+speedY < 800){
                    try {
                        x += speedX;
                        y += speedY;
                        Thread.sleep(15);
                    } catch (InterruptedException e) {
                        r = 0;
                    }
                }else if (!(x > 30 && x+2*r+speedX < 1500 )){
                    speedX = 0;
                    if ( x <= 30){
                        try {
                            x += 2;
                            Thread.sleep(15);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        try {
                            x -= 2;
                            Thread.sleep(15);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }
                } else{
                    speedY = 0;
                    if (y <= 30){
                        y += 2;
                        try {
                            Thread.sleep(15);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    } else {
                        y -= 2;
                        try {
                            Thread.sleep(15);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }

                }
            }
        }
    };
}